<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/27
 * Time: 22:51
 */
namespace app\command\once;

use app\model\SignIn;
use app\model\UserSign;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SyncUserSign extends Command
{
    protected function configure(){
        $this->setName('SyncUserSign')->setDescription("计划任务 Crontab");
    }

    protected function execute(Input $input, Output $output){

        ignore_user_abort();
        set_time_limit(0);
        ini_set('memory_limit','256M');

        $time = date('Y-m-d H:i');
        $output->writeln($time.' SyncUserSign Cron job start...');
        /*** 这里写计划任务列表集 START ***/
        $num = self::_doSync();
        $output->writeln($time.' The number of changes is '.$num);

        /*** 这里写计划任务列表集 END ***/
        $output->writeln($time.' SyncUserSign Cron job end...');
    }


    private function _doSync()
    {
        $num = 0;
        $where = [
            'sign_status' => 1,
            'sign_type' => 1,
        ];
        $signModel = new SignIn();
        $userSignModel = new UserSign();
        $signList = $signModel->getSignListByWhere($where,'*');
        if($signList){
            foreach ($signList as $v){
                $userSignData = [
                    'us_orgid' => $v['sign_orgid'],
                    'us_uid' => $v['sign_uid'],
                    'us_position' => $v['sign_position'],
                    'us_start_time' => $v['sign_time'],
                    'us_type' => $v['sign_type'],
                    'us_must' => $v['sign_must'],
                    'us_real' => $v['sign_real'],
                    'us_pic' => $v['sign_pic'],
                    'us_create_time' => $v['sign_time']
                ];
                $endWhere = [
                    ['sign_uid','=',$v['sign_uid']],
                    ['sign_type','=',2],
                    ['sign_status','=',1],
                    ['sign_time','>',$v['sign_time']],
                    ['sign_time','<=',strtotime(date('Y-m-d 23:59:59',$v['sign_time']))],
                ];
                $signEndInfo = $signModel->getSignInfoByWhere($endWhere);
                if($signEndInfo && $signEndInfo['sign_time']>0){
                    $userSignData['us_end_time'] = $signEndInfo['sign_time'];
                }

                $rt = $userSignModel->addOneSign($userSignData);
                if($rt) $num++;
            }
        }
        return $num;
    }
}